//
//  TicketOrderTicketCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/14.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "TicketOrderTicketCell.h"
#import "TicketCodeCell.h"
#import "CompeleteTicketeModel.h"

@implementation TicketOrderTicketCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self setup];
}


- (void)setup {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"TicketCodeCell" bundle:nil] forCellWithReuseIdentifier:@"TicketCodeCell"];
    
    self.collectionView.pagingEnabled = YES;
    
    self.ticketNumLabel.textColor = kSubitleColor;
    self.useCountLabel.textColor = kContentColor;
}

- (IBAction)useBtnClicked:(UIButton *)sender {
    
    if (_delegate && [_delegate respondsToSelector:@selector(goPurhchaseTipWith:)]) {
        
        [_delegate goPurhchaseTipWith:self.tickteModel.ticketingNotes];
    }
}


- (void)configDataWith:(UploadTicketOrderModel *)model{
    
    self.tickteModel = model;
   
    [self.useBtn setTitle:MYLocalizedString(@"TICKET_USING_TIP", nil) forState:UIControlStateNormal];
    
    if(model.tickets.count > 0){
         NSString * str = [NSString stringWithFormat:MYLocalizedString(@"TICKET_NUM", nil),model.tickets.count];
        self.ticketNumLabel.text = [NSString stringWithFormat:@"%@:%@",MYLocalizedString(@"TICKET_E_TICKET", nil),str];
        self.dataSource = [NSMutableArray arrayWithArray:model.tickets];
        self.currentIndex = 0;
    }
    
  
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    
    _currentIndex = currentIndex;
    
    CompeleteTicketeModel * model =  self.dataSource[currentIndex];
    
    self.useCountLabel.text = [NSString stringWithFormat:@"(%@%@)",model.size,MYLocalizedString(@"TICKET_USE_PEOPLE_COUNT", nil)];
}

- (IBAction)preBtnClicked:(UIButton *)sender {
    
    if (self.currentIndex <=0) {
        
        [IGUtil showToast:MYLocalizedString(@"TICKET_FIRST_ONE", nil)];
        return;
    }
    
    self.currentIndex = self.currentIndex - 1;
    
    NSIndexPath * path = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
                          
    [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

- (IBAction)nextBtnClicked:(UIButton *)sender {
    
    if (self.currentIndex >= self.dataSource.count - 1) {
        
        [IGUtil showToast:MYLocalizedString(@"TICKET_LAST_ONE", nil)];
        return;
    }
    
    self.currentIndex = self.currentIndex + 1;
    
    NSIndexPath * path = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
    
    [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat w = kScreenWidth - 192;
    self.currentIndex = scrollView.contentOffset.x/w;
}

#pragma mark  -- UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TicketCodeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TicketCodeCell" forIndexPath:indexPath];
    
    CompeleteTicketeModel * model = self.dataSource[indexPath.row];
    
    [cell configDataWith:model];
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat w = kScreenWidth - 192;
    
    return CGSizeMake(w, w);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.f;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
